package com.lanyou.esb.cook.proxy.service.impl;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.common.SystemResCode;
import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.Http;
import com.lanyou.esb.cook.proxy.exception.AuthException;
import com.lanyou.esb.cook.proxy.meta.AuditStatus;
import com.lanyou.esb.cook.proxy.meta.EnableType;
import com.lanyou.esb.cook.proxy.meta.IfStatus;
import com.lanyou.esb.cook.proxy.meta.IsolationStatus;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.HttpJedisProxy;
import com.lanyou.esb.cook.proxy.service.HttpService;

/**
 * HTTP接口服务
 * 
 * @author Davey.wu
 */
public class HttpServiceImpl implements HttpService {
	/** 数据库操作接口 **/
	private BaseDao<Http> dao;
	/** Redis代理接口 **/
	private HttpJedisProxy jedisProxy;

	@Override
	public void loadAllToRedis() {
		try {
			jedisProxy.loadHashAll(dao.findAll());
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public Http get(String systemCode, String ifCode, String ifVersion) {
		try {
			return jedisProxy.hget(jedisProxy.getKeyFromFields(systemCode, ifCode,
					ifVersion));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void reload(Http http) {
		// TODO Auto-generated method stub
		try {
			jedisProxy.reloadHash(http);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void validate(Http http) throws AuthException {
		if (http == null) {
			return;
		}
		// 接口不可用
		if (EnableType.DISABLE.ValueCode.equals(http.getIsEnable())) {
			if (TraceLogServiceImpl.isTrace()) {
				// 记录跟踪日志
				TraceLogServiceImpl
						.addTraceLog(SystemResCode.MSG_IF_DISABLE,
								Constants.FLAG_LEFT_SQUARE_BRACKET,
								Constants.IS_ENABLE, Constants.FLAG_EQUAL,
								http.getIsEnable(),
								Constants.FLAG_RIGHT_SQUARE_BRACKET);
			}
			throw new AuthException(SystemResCode.MSG_IF_DISABLE,
					SystemResCode.CODE_IF_DISABLE);
		}
		// 接口隔离
		if (IsolationStatus.ISOLATION.ValueCode.equals(http.getIsIsolation())) {
			if (TraceLogServiceImpl.isTrace()) {
				// 记录跟踪日志
				TraceLogServiceImpl.addTraceLog(SystemResCode.MSG_ISOLATION,
						Constants.FLAG_LEFT_SQUARE_BRACKET,
						Constants.ISOLATION, Constants.FLAG_EQUAL,
						http.getIsIsolation(),
						Constants.FLAG_RIGHT_SQUARE_BRACKET);
			}
			throw new AuthException(SystemResCode.MSG_ISOLATION,
					SystemResCode.CODE_ISOLATION);
		}

		// 接口未审核
		if (AuditStatus.UNAUDITED.ValueCode.equals(http.getAuditStatus())) {
			if (TraceLogServiceImpl.isTrace()) {
				// 记录跟踪日志
				TraceLogServiceImpl.addTraceLog(SystemResCode.MSG_UNAUDITED,
						Constants.FLAG_LEFT_SQUARE_BRACKET,
						Constants.AUDIT_STATUS, Constants.FLAG_EQUAL,
						http.getAuditStatus(),
						Constants.FLAG_RIGHT_SQUARE_BRACKET);
			}
			throw new AuthException(SystemResCode.MSG_UNAUDITED,
					SystemResCode.CODE_UNAUDITED);
		}
		// 接口驳回
		if (AuditStatus.REJECT.ValueCode.equals(http.getAuditStatus())) {
			if (TraceLogServiceImpl.isTrace()) {
				// 记录跟踪日志
				TraceLogServiceImpl.addTraceLog(SystemResCode.MSG_REJECT,
						Constants.FLAG_LEFT_SQUARE_BRACKET,
						Constants.AUDIT_STATUS, Constants.FLAG_EQUAL,
						http.getAuditStatus(),
						Constants.FLAG_RIGHT_SQUARE_BRACKET);
			}
			throw new AuthException(SystemResCode.MSG_REJECT,
					SystemResCode.CODE_REJECT);
		}
		// 接口停用
		if (IfStatus.DISABLE.ValueCode.equals(http.getStatus())) {
			if (TraceLogServiceImpl.isTrace()) {
				// 记录跟踪日志
				TraceLogServiceImpl.addTraceLog(SystemResCode.MSG_IF_UNUSE,
						Constants.FLAG_LEFT_SQUARE_BRACKET, Constants.STATUS,
						Constants.FLAG_EQUAL, http.getStatus(),
						Constants.FLAG_RIGHT_SQUARE_BRACKET);
			}
			throw new AuthException(SystemResCode.MSG_IF_UNUSE,
					SystemResCode.CODE_IF_UNUSE);
		}

	}

	public HttpJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(HttpJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	public BaseDao<Http> getDao() {
		return dao;
	}

	public void setDao(BaseDao<Http> dao) {
		this.dao = dao;
	}
}
